﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace SAD
{
    public partial class popupUbicacion : Form
    {
        public popupUbicacion()
        {
            InitializeComponent();
        }

        static private Conexion conexionBD = new Conexion();

        static string nuevoid = "";
        static string ubicacion = "";

        static private string GenerarID()
        {
            String consulta = "SELECT SUBSTRING(MAX(idUbicacion),4,9) FROM UBICACION";
            SqlDataReader dataReader = conexionBD.EjecutarReader(consulta);
            dataReader.Read();
            string aux = dataReader[0] + "";
            int r;
            if (aux == "") r = 1;
            else r = Convert.ToInt32(aux) + 1;

            string id = r.ToString();
            int nrodig = id.Length;

            for (int i = 1; i <= 9 - nrodig; i++)
                id = "0" + id;
            id = "UBI" + id;
            dataReader.Close();
            return id;
        }

        private void btn_guardar_Click(object sender, EventArgs e)
        {

            string consulta = "";

            nuevoid = GenerarID();

            string calle = txt_calle.Text;
            string numero = txt_num.Text;
            string mzlt = txt_mzl.Text;
            string conjviv = txt_cv.Text;
            string grupsec = txt_gs.Text;

            string dpto = cbx_dpto.Text;
            string prov = cbx_prov.Text;
            string dist = cbx_dist.Text;

            if (dpto != "")
            {
                if (prov != "")
                {
                    if (dist != "")
                        consulta = "INSERT INTO UBICACION (IDUBICACION, CALLE, NUMERO, CONJUNTOVIVIENDA, MANZANA, SECTORGRUPO, DISTRITO, PROVINCIA, DEPARTAMENTO)" +
                                          "VALUES ( " + "\'" + @nuevoid + "\'" + "," + "\'" + @calle + "\'" + "," + "\'" + @numero + "\'" + "," + "\'" + @conjviv + "\'" + "," + "\'" + @mzlt + "\'" + ","
                                          + "\'" + @grupsec + "\'" + "," + "\'" + @dist + "\'" + "\'" + @prov + "\'" + "," + "\'" + @dpto + "\'" + ")";
                    
                }

            }

            ubicacion = calle + " " + numero;
            conexionBD.EjecutarComando(consulta);
            MessageBox.Show("Insert exitoso", "SAD Aviso"); //Decidir sobre messageBox

            this.Close();
        }

        private void btn_cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbx_dpto_SelectedIndexChanged(object sender, EventArgs e)
        {
            String consulta = "SELECT PROVINCIA FROM UBICACION WHERE DEPARTAMENTO LIKE '%" + cbx_dpto.Text + "'%";
            SqlDataReader dataReader = conexionBD.EjecutarReader(consulta);
            int i = 0;
            while (dataReader.Read())
            {
                cbx_dpto.Items.Add(dataReader[i].ToString());
                i++;
            }

            dataReader.Close();
        }

        private void cbx_prov_SelectedIndexChanged(object sender, EventArgs e)
        {
            String consulta = "SELECT DISTRITO FROM UBICACION WHERE DEPARTAMENTO LIKE '%" + cbx_dpto.Text + "%' AND PROVINCIA LIKE '%" + cbx_prov.Text + "%'";
            SqlDataReader dataReader = conexionBD.EjecutarReader(consulta);
            int i = 0;
            while (dataReader.Read())
            {
                cbx_dpto.Items.Add(dataReader[i].ToString());
                i++;
            }

            dataReader.Close();
        }

        private void cbx_dist_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void popupUbicacion_Load(object sender, EventArgs e)
        {
            String consulta = "SELECT DEPARTAMENTO FROM UBICACION";
            SqlDataReader dataReader = conexionBD.EjecutarReader(consulta);
            int i = 0;
            while (dataReader.Read())
            {
                cbx_dpto.Items.Add(dataReader[i].ToString());
                i++;
            }
            dataReader.Close();
        }

    }
}
